package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;


import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.HisDetailActivity;
import com.xgkj.diyiketang.data.AudieceInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/21.
 */
public class AudieceAdapter extends BaseAdapter {
    private Context mContext;
    private List<AudieceInfo> mList = new ArrayList<AudieceInfo>();

    public AudieceAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<AudieceInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.audie_item, null);
            childHolder.headImg = (ImageView) convertView.findViewById(R.id.audie_img);
            childHolder.headImg.setLayoutParams(new RelativeLayout.LayoutParams(ViewUtil.dip2px(mContext,32),ViewUtil.dip2px(mContext,32)));
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        final AudieceInfo fansInfo = mList.get(position);
        if (fansInfo != null) {
            String headImgUrl = fansInfo.getAudienceLogo();

            if (headImgUrl != null && headImgUrl.length() > 0) {
                ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(mContext, 32), ViewUtil.dip2px(mContext, 32));
                String cacheKey = ImageCache.getInstance(mContext).getCacheKey(headImgUrl,size);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(cacheKey);
                if (cacheBitmap != null){
                    childHolder.headImg.setImageBitmap(cacheBitmap);
                }else {
                    childHolder.headImg.setImageResource(R.drawable.default_head_image);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.headImg, headImgUrl, size);
                }
            }

            childHolder.headImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, HisDetailActivity.class);
                    intent.putExtra("USERID",fansInfo.getAudienceId());
                    mContext.startActivity(intent);
                }
            });
        }
//
//        childHolder.headImg.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent bigtIntent = new Intent(mContext,XiangceBigActivity.class);
////                        bigtIntent.putExtra("photoInfo",finfo);
//                bigtIntent.putExtra("photoInfo",(ArrayList<PhotoInfo>)mList);
//                bigtIntent.putExtra("currentPosition",position);
//                bigtIntent.putExtra("FROM",1);
//                mContext.startActivity(bigtIntent);
//            }
//        });

        return convertView;
    }

    class ChildHolder {
        ImageView headImg;
    }
}
